/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
 **/
define("pages/ccic/ccic_query", [
        "../../common/componts.js",
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        "../../plugins/artTemplate/artTemplate",
        "../../common/tools"
    ],
    function (require) {
        var componts = require("../../common/componts");
        var queryTableId = "awdQueryTableId";
        var selectid;

        var getQueryDataGrid = function () {
            return $("#" + queryTableId);
        };

        var tableInit = function () {
            componts.initTable(queryTableId, {
                url: "/ccic/list",
                title: '',
                fit: true,
                // firstLoad:true,
                width: '100%',
                columns: [
                    {
                        field: 'id',
                        title: '序号',
                        width: 30,
                        formatter: function (value, rowData, rowIndex) {
                            return '<input type="radio" name="ryRadio" id="ryRadio' + rowIndex + '" value="' + rowData.id + '" />';
                        }
                    }
                    , {
                        field: 'xm', title: '姓名', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'bm', title: '别名', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'zjhm', title: '证件号码', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'xbString', title: '性别', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'csrqString', title: '出生日期', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'ztrqString', title: '在逃日期', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'operate', title: '详细信息', align: 'center', width: 60,
                        formatter: function (value, row, index) {
                            return '<a href="#" data-target="#ckPhoto" value="' + index + '" data-toggle="modal" class="easyui-linkbutton awd-query-view-detail"><img src="../../img/xq.png"></a>';
                        }
                    }
                ],
                //基本信息双击事件
                onDblClickRow: function (rowIndex, rowData) {
                    $("input[name='ryRadio']")[rowIndex].checked = true;
                    getQueryDataGrid().datagrid("selectRow", rowIndex);
                    selectid = rowData.id;
                },
                onLoadSuccess: function (data) {
                    if (data.total > 0) {
                        getQueryDataGrid().datagrid("selectRow", 0);
                        $("input[name='ryRadio']")[0].checked = true;
                    }
                    //$("a[name='opera']").each(function(){
                    //    $(this).on('click',function () {
                    //        var index = $(this).attr('value');
                    //        var jbxx = data.rows[index];
                    //        kss.initJbxxxx(jbxx.gcbh,jbxx.rybh,jbxx.xm+"--详细信息");
                    //    });
                    //});
                },
                //单击行选中取消事件
                onClickRow: function (rowIndex, rowData) {
                    if (selectid != rowData.id) {
                        $("input[name='ryRadio']")[rowIndex].checked = true;
                        selectid = rowData.id;
                    } else {
                        getQueryDataGrid().datagrid("unselectRow", rowIndex);
                        $("input[name='ryRadio']")[rowIndex].checked = false;
                        getQueryDataGrid().datagrid('clearSelections');
                        selectid = "";
                    }
                }
            });
            //页面高度自适应
            getQueryDataGrid().datagrid('resize', {
                height: ($(window).height() - 80)
            });
        };

        /**
         * 数据列表刷新加载
         */
        var loadDataGrid = function () {
            var formData = $(".awd-query-form").serializeObject();
            var queryParams = $.extend({}, formData, {
                "state": "R8",
                "processDefinitionKey": componts.getUrlParam('processDefinitionKey'),
                "taskDefinitionKey": componts.getUrlParam('taskDefinitionKey'),
                "pageSize": function () {
                    return getQueryDataGrid().datagrid("getPager").pagination("options").pageSize;
                },
                "pageIndex": function () {
                    return getQueryDataGrid().datagrid("getPager").pagination("options").pageNumber;

                }
            });
            getQueryDataGrid().datagrid('clearSelections');
            getQueryDataGrid().datagrid('load', queryParams);
        };

        var bindAction = function () {
            //查询
            $(".query-btn").on("click", function () {
                loadDataGrid();
            });
            //打印
            $(".print-btn").on("click", function () {
                getQueryDataGrid().datagrid('print', {"title": "CCIC查询"});
            });
            //导出Excel
            $(".export-btn").on("click", function () {
                getQueryDataGrid().datagrid('toExcel', {"filename": "CCIC查询.xls"});
            });

            //单击行详情
            $(document).on('click', '.awd-query-view-detail', function () {
                console.log('awd-query-view-detail', $(this).attr("value"));
            });

            /**
             * 绑定回车事件
             */
            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    loadDataGrid();
                }
            });

        };

        //初始化
        (function () {
            componts.initSelectPage(undefined);
            componts.initSelect(undefined, undefined, undefined);
            tableInit();
            bindAction();
        })()
    });
